home *** CD-ROM | disk | FTP | other *** search
- /*
- * @(#)set.c 1.3 7/9/87
- */
- /* Copyright 1986 Norm Hutchinson. May not be used for any *
- * purpose without written permission from the author. */
-
- #include "assert.h"
- #include "system.h"
- #include "set.h"
-
- #define INITIALSIZE 4
-
- #define HASH(key, set) (((key) ^ ((key) >> 4)) & set->maxIndex)
- static void SetCheckOutHashTable();
-
- Set Set_Create()
- {
- register int i;
- register Set m;
-
- m = (Set) malloc(sizeof(SetRecord));
- m->maxIndex = INITIALSIZE - 1;
- m->maxCount = m->maxIndex - (m->maxIndex >> 2); /* Max fill 87.5 % */
- m->count = 0;
- m->table = (STEPtr) malloc(INITIALSIZE * sizeof(STE));
- for (i = 0; i < INITIALSIZE; i++) {
- m->table[i].key = NIL;
- }
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- return(m);
- }
-
- Set Set_CreateSized(fCount)
- int fCount;
- {
- register int i;
- register Set m;
-
- assert(fCount > 0);
- m = (Set) malloc(sizeof(SetRecord));
- for (i = 1; i < fCount; i += i);
- m->maxIndex = i - 1;
- m->maxCount = m->maxIndex - (m->maxIndex >> 2); /* Max fill 87.5 % */
- m->count = 0;
- m->table = (STEPtr) malloc((unsigned)((m->maxIndex+1) * sizeof(STE)));
- for (i = 0; i <= m->maxIndex ; i++) {
- m->table[i].key = NIL;
- }
- #ifdef lint
- SetCheckOutHashTable(m);
- #endif
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- return(m);
- }
-
- void Set_Destroy(m)
- register Set m;
- {
- if (m == (Set) NULL) return;
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- free((char *)m->table);
- free((char *)m);
- }
-
- static void ExpandHashTable(m)
- register Set m;
- {
- register STE *nh, *oe, *ne;
- register int oldHashTableSize = m->maxIndex + 1, i;
- register int key;
- int index;
-
- m->maxIndex = oldHashTableSize * 2 - 1;
- m->maxCount = m->maxIndex - (m->maxIndex >> 2);
- nh = (STEPtr) malloc((unsigned)(oldHashTableSize * 2 * sizeof(STE)));
- for (i = 0; i <= m->maxIndex; i++) nh[i].key = NIL;
- for (i = 0; i < oldHashTableSize; i++) {
- oe = &m->table[i];
- key = oe->key;
- if (key == NIL) continue;
- index = HASH(key, m);
- while (1) {
- ne = &nh[index];
- if (ne->key == NIL) {
- ne->key = oe->key;
- break;
- } else {
- assert(ne->key !=key);
- index++;
- if (index > m->maxIndex) index = 0;
- }
- }
- }
- free((char *)m->table);
- m->table = nh;
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- }
-
- int Set_Lookup(m, key)
- register Set m;
- register int key;
- {
- register int index = HASH(key, m);
- register STEPtr e;
-
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- while (1) {
- e = &m->table[index];
- if (e->key == NIL) { /* we did not find it */
- return (0);
- } else if (e->key == key) {
- return (1);
- }
- if (++index > m->maxIndex) index = 0;
- }
- }
-
- void Set_Delete(m, key)
- register Set m;
- register int key;
- /* Remove the entry, if it is there */
- {
- register int index = HASH(key, m);
- register STEPtr e;
-
- while (1) {
- e = &m->table[index];
- if (e->key == NIL) { /* we did not find it */
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- return;
- }
- if (e->key == key) {
- /* Found it, now remove it */
- m->count--;
- e->key = NIL;
- while (1) {
- /* rehash until we reach nil again */
- if (++index > m->maxIndex) index = 0;
- e = & m->table[index];
- key = e->key;
- if (key == NIL) {
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- return;
- }
- /* rehashing is done by removing then reinserting */
- e->key = NIL;
- m->count--;
- Set_Insert(m, key);
- }
- }
- if (++index > m->maxIndex) index = 0;
- }
- }
-
- void Set_Insert(m, key)
- register Set m;
- register int key;
- {
- register int index;
- register STEPtr e;
- assert(key != NIL);
- if (m->count >= m->maxCount) ExpandHashTable(m);
- index = HASH(key, m);
- while (1) {
- e = &m->table[index];
- if (e->key == NIL) { /* put it here */
- e->key = key;
- m->count++;
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- return;
- } else if (e->key == key) {
- #ifdef DEBUGSET
- SetCheckOutHashTable(m);
- #endif DEBUGSET
- return;
- }
- if (++index > m->maxIndex) index = 0;
- }
- }
-
- int Set_FindNext(m, startIndex, keyPtr)
- register Set m;
- int *startIndex;
- int *keyPtr;
- {
- register int i;
- register STEPtr e;
- if (m == NULL) return(0);
- for (i = *startIndex; i <= m->maxIndex; i++) {
- e = &m->table[i];
- if (e->key != NIL) {
- *keyPtr = e->key;
- *startIndex = i + 1;
- return(1);
- }
- }
- *keyPtr = NIL;
- *startIndex = -1;
- return(0);
- }
-
- void Set_Merge(from, to)
- Set from, to;
- {
- int k;
- Set_For(k, from)
- Set_Insert(to, k);
- Set_Next
- }
-
- void Set_Print(m)
- register Set m;
- {
- int key, index;
- printf(
- "\nDump of set @ 0x%05x, %d entr%s, current max %d\nIndex\tKey\n",
- m, m->count, m->count == 1 ? "y" : "ies", m->maxCount);
- for (index = 0; index <= m->maxIndex; index++) {
- key = m->table[index].key;
- printf("%3d\t0x%08.8x\n", index, key);
- }
- }
-
- static void SetCheckOutHashTable(m)
- register Set m;
- {
- register int i;
- register STEPtr realElement, e;
- register int index, firstIndex, count;
- count = 0;
-
- for (i = 0; i <= m->maxIndex; i++) {
- realElement = &m->table[i];
- if (realElement->key != NIL) {
- count++;
- index = HASH(realElement->key, m);
- firstIndex = index;
- while (1) {
- e = &m->table[index];
- if (e->key == NIL) { /* we did not find it */
- break;
- } else if (e->key == realElement->key) {
- break;
- } else {
- index++;
- if (index > m->maxIndex) index = 0;
- if (index == firstIndex) {
- index = -1;
- break;
- }
- }
- }
-
- if (index == -1 || e->key != realElement->key) {
- fprintf(stderr,
- "Set problem: Key 0x%x, rightIndex %d, realIndex %d\n",
- realElement->key, firstIndex, index);
- Set_Print(m);
- }
- }
- }
- if (count != m->count) {
- fprintf(stderr,
- "Set problem: Should have %d entries, but found %d.\n", m->count,
- count);
- Set_Print(m);
- }
- }
-